/**
 * OW2 FraSCAti Examples : HelloWorld POJO
 * Copyright (C) 2008-2010 INRIA, University of Lille 1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Haderer Nicolas
 *
 * Contributor(s): 
 */
package fr.univlille1.pji96.basex.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;

import org.basex.server.ClientQuery;
import org.basex.server.ClientSession;
import org.osoa.sca.annotations.EndsConversation;
import org.osoa.sca.annotations.Scope;

import fr.univlille1.pji96.basex.api.IDatabase;



@Scope("CONVERSATION")
public class BasexImpl implements IDatabase{

	
	private ClientSession session;
	
	public void initSession(String host, String port, String user,
			String password) throws Exception {
		
		this.session = new ClientSession(
				host,
				Integer.valueOf(port),
				user,
				password);
	}
	
	@EndsConversation
	public void endSession() throws Exception {
		if (session != null)
			session.close();
	}

	
	public String commmand(String command) throws Exception {
		return session.execute(command);
	}
	
	
	
	public void create(String name) throws Exception {
		
		 if (exist(name))
		    	throw new Exception("Database "+name+" already exist");
		    
		    session.execute("CREATE DB "+name);
		    session.execute("CREATE INDEX fulltext");
		
	}

	public Boolean exist(String name) throws Exception {
		return false;
	}

	public void open(String name) throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void close() throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void drop(String name) throws Exception {
		// TODO Auto-generated method stub
		
	}

	public String query(String query) throws Exception {
		
		return session.execute("XQUERY "+query);
	}

	public String query(String[] argNames, String[] argValue,
			String[] argTypes, String query) throws Exception {
		
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		final ClientQuery cQuery = new ClientQuery(query, session,out); 
		for (int i=0;i<argNames.length;i++){
			cQuery.bind(argNames[i], argValue[i],argTypes[i]);
		}
		return cQuery.execute();
		//cQuery.execute();
		//return new String(out.toByteArray());
		
	}

	public void insertFile(File file) throws Exception {
		session.execute("ADD "+file.getAbsolutePath());
	}

	

	public void grant(String user, String permission, String dbName)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void createUser(String username, String password) throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void dropUser(String user) throws Exception {
		// TODO Auto-generated method stub
		
	}

	
	
	


}
